package com.elf.zero.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.elf.zero.app.R;


public class RoundShadeWidget extends FrameLayout {

    private int topLeftRadius = 30;        //leftTopRadians
    private int topRightRadius = 30;     //leftBottomRadians
    private int bottomLeftRadius = 30;       //rightTopRadians
    private int bottomRightRadius = 30;    //rightBottomRadians

    private Bitmap shadeBitmap;
    private Paint paint;
    private RectF rectF;

    public RoundShadeWidget(Context context) {
        this(context, null, 0);
    }

    public RoundShadeWidget(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundShadeWidget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setBackgroundColor(Color.TRANSPARENT);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundShadeWidget);

        int radius = typedArray.getDimensionPixelOffset(R.styleable.RoundShadeWidget_zero_radius, -1);
        if (radius != -1) {
            topLeftRadius = radius;
            topRightRadius = radius;
            bottomLeftRadius = radius;
            bottomRightRadius = radius;
        } else {
            topLeftRadius = typedArray.getDimensionPixelSize(R.styleable.RoundShadeWidget_zero_topLeftRadius, topLeftRadius);
            topRightRadius = typedArray.getDimensionPixelSize(R.styleable.RoundShadeWidget_zero_topRightRadius, topRightRadius);
            bottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.RoundShadeWidget_zero_bottomLeftRadius, bottomLeftRadius);
            bottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.RoundShadeWidget_zero_bottomRightRadius, bottomRightRadius);
        }

        typedArray.recycle();

        paint = new Paint();
    }

    public void setRadius(int radius) {
        setRadians(radius, radius, radius, radius);
    }

    public void setRadians(int topLeftRadius, int topRightRadius, int bottomLeftRadius, int bottomRightRadius) {
        this.topLeftRadius = topLeftRadius;
        this.topRightRadius = topRightRadius;
        this.bottomLeftRadius = bottomLeftRadius;
        this.bottomRightRadius = bottomRightRadius;
    }

    /**
     *
     */
    private Bitmap createArcBitmap(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.

        c.drawArc(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2), 180, 90, true, p);
        c.drawArc(new RectF(0, getHeight() - bottomLeftRadius * 2, bottomLeftRadius * 2, getHeight()), 90, 90, true, p);
        c.drawArc(new RectF(getWidth() - topRightRadius * 2, 0, getWidth(), topRightRadius * 2), 270, 90, true, p);
        c.drawArc(new RectF(getWidth() - bottomRightRadius * 2, getHeight() - bottomRightRadius * 2, getWidth(), getHeight()), 0, 90, true, p);
        return bm;
    }

    /**
     *
     */
    private Bitmap createRectBimap(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.WHITE);

        c.drawRect(new RectF(0, 0, topLeftRadius, topLeftRadius), p);
        c.drawRect(new RectF(0, getHeight() - bottomLeftRadius, bottomLeftRadius, getHeight()), p);
        c.drawRect(new RectF(getWidth() - topRightRadius, 0, getWidth(), topRightRadius), p);
        c.drawRect(new RectF(getWidth() - bottomRightRadius, getHeight() - bottomRightRadius, getWidth(), getHeight()), p);
        return bm;
    }

    private Bitmap createShadeBitmap() {
        Bitmap src = createArcBitmap(getWidth(), getHeight());

        Canvas canvas = new Canvas(src);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(false);
        paint.setStyle(Paint.Style.FILL);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        //then draw rect-src-bitmap
        canvas.drawBitmap(createRectBimap(getWidth(), getHeight()), 0, 0, paint);

        return src;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        shadeBitmap = createShadeBitmap();
    }


    @Override
    public void draw(Canvas canvas) {

        if (rectF == null) {
            rectF = new RectF(0, 0, getWidth(), getHeight());
        }
        rectF.left = 0;
        rectF.top = 0;
        rectF.right = getWidth();
        rectF.bottom = getHeight();
        canvas.saveLayer(rectF, null, Canvas.ALL_SAVE_FLAG);

        super.draw(canvas);

        if (shadeBitmap == null) {
            shadeBitmap = createShadeBitmap();
        }

        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        canvas.drawBitmap(shadeBitmap, 0, 0, paint);
    }

}
